/*
  Copyright 2015 Dell Inc.
  ALL RIGHTS RESERVED.
*/
package com.kikcn.demo;

import com.kikcn.demo.domain.Role;
import com.kikcn.demo.services.RoleService;
import com.mysql.jdbc.log.Log;
import net.sf.ehcache.Element;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;


public class InitBean implements  ApplicationContextAware {
public static final Logger logger=Logger.getLogger(InitBean.class);

@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
    logger.info("Initializing security information start.");
    RoleService roleService=(RoleService)context.getBean("roleService");
    List<Role> roles=roleService.findAll();
    CacheManager cacheManager=(CacheManager )context.getBean("demoEhcacheManager");
    Cache cache=cacheManager.getCache("demoCache");
    for(Role role: roles){
        Element element=new Element("Role_"+role.getId(),role.getResources());
        cache.put(element);
    }
    logger.info("Initializing security information done.");

}
}
